/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 4, 2012  5:52:02 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : SdCustomerOutb1Services.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.SdCustomerOutb1;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class SdCustomerOutb1Services extends JPAService {
	
	
	private static final Logger log = Logger.getLogger(SdCustomerOutb1Services.class);

	/**
	 * @param em
	 */
	public SdCustomerOutb1Services(EntityManager em) {
		super(em);
	}
	
	public List<SdCustomerOutb1> findByCustomerCode(String customerCode, String status) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("customerCode", customerCode);
		mapWhereValue.put("status", status);
		
		return super.ConvertToYoureObject(super.find("SdCustomerOutb1.findByCustomerCode1", mapWhereValue));			
	}
	
	public SdCustomerOutb1 findById(String id) throws Exception {				
		
		return super.ConvertToYoureObject(super.find(id, "SdCustomerOutb1.findById"));				
	}
	
	public List<SdCustomerOutb1> findAll() throws Exception {		
		
		return super.ConvertToYoureObject(super.findAll("SdCustomerOutb1.findAll"));				
	}
	
	public List<SdCustomerOutb1> findAll(int pageSize, int pageNum) throws Exception {		
		super.setPageSize(pageSize);
		return super.ConvertToYoureObject(super.findAll("SdCustomerOutb1.findAll", pageNum));
	}
	
	public List<SdCustomerOutb1> findAllMaxResult(int maxResult) throws Exception {		
		return super.ConvertToYoureObject(super.findAllMaxResult("SdCustomerOutb1.findAll", maxResult));
	}
	
	public List<SdCustomerOutb1> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "SdCustomerOutb1.findByInstanceNo"));				
	}
	
	public List<SdCustomerOutb1> findByInstanceNo(String instanceNo, int pageSize, int pageNum) throws Exception {		
		super.setPageSize(pageSize);
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "SdCustomerOutb1.findByInstanceNo", pageNum));
	}
	
	public List<SdCustomerOutb1> findAllIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("SdCustomerOutb1.findAllIsSync", mapWhereValue));			
	}
	
	public List<SdCustomerOutb1> findByCustomerInstanceNo(String customerCode, String instanceNo) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("customerCode", customerCode);
		mapWhereValue.put("instanceNo", instanceNo);
		
		return super.ConvertToYoureObject(super.find("SdCustomerOutb1.findByInstanceNo1", mapWhereValue));			
	}

	public List<SdCustomerOutb1> findAllIsSync(String issync, int pageSize, int pageNum) throws Exception {		
		super.setPageSize(pageSize);
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("SdCustomerOutb1.findAllIsSync", mapWhereValue, pageNum));				
	}

	public List<SdCustomerOutb1> findByCustomerCode(String customerCode, String status, String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("customerCode", customerCode);
		mapWhereValue.put("status", status);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("SdCustomerOutb1.findByCustomerCode", mapWhereValue));			
	}
	
	public List<SdCustomerOutb1> findByCustomerCode3(String customerCode, String instanceNo) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("customerCode", customerCode);
		mapWhereValue.put("instanceNo", instanceNo);
		
		return super.ConvertToYoureObject(super.find("SdCustomerOutb1.findByCustomerCode3", mapWhereValue));			
	}
	
	public List<SdCustomerOutb1> findByCustomerCode(String customerCode, String status, String issync, int pageSize, int pageNum) throws Exception {		
		super.setPageSize(pageSize);
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("customerCode", customerCode);
		mapWhereValue.put("status", status);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("SdCustomerOutb1.findByCustomerCode", mapWhereValue, pageNum));			
	}
	
	public SdCustomerOutb1 Updated(SdCustomerOutb1 value) throws Exception {
		if(value != null)
		{
			return (SdCustomerOutb1) super.updated(value);
		}else
			return null;
	}
	
	public SdCustomerOutb1 InsertedOrUpdated(SdCustomerOutb1 vlaue) throws Exception {
		SdCustomerOutb1 obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("SD_CUSTOMER_OUTB1");					
				obj.setPromisStagingId(id);							
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<SdCustomerOutb1> Inserted(List<SdCustomerOutb1> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SdCustomerOutb1 obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("SD_CUSTOMER_OUTB1");					
					obj.setPromisStagingId(id);								
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					common_lst.add(obj);
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<SdCustomerOutb1> Updated(List<SdCustomerOutb1> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SdCustomerOutb1 obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());					
					common_lst.add(obj);					
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

}
